package com.exampletest.config;

import org.springframework.web.servlet.HandlerInterceptor;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        String uri = request.getRequestURI();
        // 允许访问登录、注册页面和静态资源
        if (uri.contains("/user/login") || uri.contains("/user/register") || uri.equals("/") || uri.contains("/css/") || uri.contains("/js/") || uri.contains("/images/")) {
            return true;
        }
        if (session.getAttribute("user") == null) {
            response.sendRedirect("/user/login");
            return false;
        }
        return true;
    }
} 